<?php

ignore_user_abort(true); 		// 后台运行，不受前端断开连接影响
set_time_limit(0); 				// 脚本将永久运行
chdir(dirname(__FILE__));
error_reporting(E_ERROR);
require_once ("../../config.php"); // 引入配置
require_once ("../../log.php");
require_once ("../api_tool.php");
require_once ("core.php");
/**
 * @name 机器人淘宝发单Api接口
 * @desc 本接口列表，只是用于处理当前机器人行为
 * 
 */

// 获取商品数据
if ($_GET['shop_code'] == '1') {
	$shop_data = simQuery("SELECT * FROM item_ordinary WHERE item_id = {$_GET['shop_id']} AND channel_id = {$_GET['channel_id']};");		// 获取普通库
} else {
	$shop_data = simQuery("SELECT * FROM item_preferred WHERE item_id = {$_GET['shop_id']} AND channel_id = {$_GET['channel_id']};");		// 获取优选库
}

// LOGINFO($shop_data, '=== $shop_data');
if (empty($shop_data)) {
	echo json_encode(array (
		'state' => -1,
		'msg'	=> '不是当前数据库查出的商品!',
	));
	exit();
}

// 查询是否有着商品号
$item_data = getTaobaoTbkItemInfo($shop_data['item_id']);
if (empty($item_data) || $item_data['sub_msg'] == "无结果" ) {
	// 清理优选库内容
	if ($_GET['shop_code'] == '2') {
		simQuery("DELETE FROM item_preferred WHERE id = {$_GET['shop_id']};");
	}

	echo json_encode(array (
		'state' => -2,
		'msg'	=> '不存在该商品!',
	));

	exit();
}

$wx_data = simQuery("SELECT *, wx.id AS wx_id, wx.taobao_ance AS taobao_ance, wx.pdd_ance AS pdd_ance, wx.jd_ance AS jd_ance FROM wechat_group AS wx, robot AS bot WHERE wx.id = {$_GET['wx_id']} AND wx.robot_id = bot.id");
if (empty($wx_data)) {
	echo json_encode(array (
		'state' => -3,
		'msg'	=> '不存在当前微信群!',
	));
	exit(); 
}

if (empty($item_data['results']) || empty($item_data['results']['n_tbk_item'])) {
	// 清理优选库内容
	if ($_GET['shop_code'] == '2') {
		simQuery("DELETE FROM item_preferred WHERE id = {$_GET['shop_id']};");
	}
	echo json_encode(array (
		'state' => -4,
		'msg'	=> '当前商品详细数据为空!',
	));
	exit();
}
// 是否能正常生成淘口令
$share_sheft_url_info = getTaokoulingTbkPrivilege($shop_data['item_id'], "110126350144", "1382650485", "2207473775157", $wx_data['taobao_ance']);
$share_sheft_url = $share_sheft_url_info['result']['data']['coupon_click_url'];
if (empty($share_sheft_url)) {
	if ($_GET['shop_code'] == '2') {
		simQuery("DELETE FROM item_preferred WHERE id = {$_GET['shop_id']};");
	}
	echo json_encode(array (
		'state' => -5,
		'msg'	=> '淘口令生成失败!',
	));
	exit();
}
$Tpwd = createTaobaoTbkTpwd($wx_data['taobao_ance'], "美淘优惠券口令", $share_sheft_url)['data']['model'];

if (empty($Tpwd)) {
	if ($_GET['shop_code'] == '2') {
		simQuery("DELETE FROM item_preferred WHERE id = {$_GET['shop_id']};");
	}
	echo json_encode(array (
		'state' => -5,
		'msg'	=> '淘口令生成失败!',
	));
	exit();
}

$data = array();

foreach($item_data['results']['n_tbk_item'] as $key => $value) {
	if (!is_int($key) && !is_array($value) && !empty($value)) {
		$data[$key] = $value;
	}
}

foreach($shop_data as $key => $value) {
	if (!is_int($key) && !is_array($value) && !empty($value)) {
		$data[$key] = $value;
	}
}

$i = 1;
foreach ($item_data['results']['n_tbk_item']['small_images']['string'] as $value) {
	$data['small_images'.$i] = $value;
	++$i;
}

foreach ($wx_data as $key => $value) {
	if (!is_int($key) && !is_array($value) && !empty($value)) {
		$data[$key] = $value;
	}
}

// 商品记录选取一下组合成数据发送
$data['good_img'] = $_GET['good_img'];
$host = "127.0.0.1";
$port = 80;
$url = "/api/api_taobao/send.php";

simQuery("UPDATE wechat_group SET wait_time = 0 WHERE id = {$wx_data['wx_id']}");
sendFZSHttp($host, $port, $url, $data);
echo json_encode(array (
	'state' => 1,
	'msg'	=> '成功!',
));
exit;